package friend

import (
	"api/common"
	"api/pb/friend"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetFriendGroupListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetFriendGroupListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFriendGroupListLogic {
	return &GetFriendGroupListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// GetFriendGroupList 获取好友分组列表
func (l *GetFriendGroupListLogic) GetFriendGroupList(req *types.FriendGroupListRequest) (resp *types.FriendGroupListResponse, err error) {
	userID, err := common.GetUserIDFromCtx(l.ctx)
	if err != nil {
		return nil, err
	}

	result, err := l.svcCtx.FriendRPC.GetGroupList(l.ctx, &friend.GroupListRequest{UserId: userID, Offset: req.Offset, Limit: req.Limit})
	if err != nil {
		return nil, err
	}

	resp = &types.FriendGroupListResponse{
		Total: result.Total,
		Data:  make([]types.FriendGroupInfo, 0),
	}
	for _, item := range result.Data {
		resp.Data = append(resp.Data, types.FriendGroupInfo{
			ID: item.Id, Name: item.Name, Sort: item.Sort,
		})
	}
	return resp, nil
}
